package com.itheima.controller;


import com.itheima.dto.MessageConstant;
import com.itheima.dto.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingServie;
import com.itheima.service.impl.OrderSettingServieImpl;
import com.itheima.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {


    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderSettingServie orderSettingServie;
    /*
     * 批量导入
     * */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) {

        List<String[]> data = null;//使用POI解析表格数据
        try {
            data = POIUtils.readExcel(excelFile);

            List<OrderSetting> lsit = new ArrayList<>();
            for (String[] strings : data) {
                String orderDate=strings[0];
                String number=strings[1];

                OrderSetting orderSetting=new OrderSetting(new Date(orderDate),Integer.parseInt(number));
                lsit.add(orderSetting);
            }
            orderSettingServie.add(lsit);
            return Result.success(lsit, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.success( MessageConstant.IMPORT_ORDERSETTING_FAIL);

        }

    }
    //根据月份查询对应的设置数据
    @PostMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {//date格式为年-月

        List<Map> list = orderSettingServie.getOrderSettingByMonth(date);
        return Result.success(list, MessageConstant.GET_ORDERSETTING_SUCCESS);
    }




    //给定的日期如果有预约设置则编辑，无则新增
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {//date格式为年-月

        orderSettingServie.editNumberByDate(orderSetting);
        return Result.success( MessageConstant.ORDERSETTING_SUCCESS);
    }





}